//
//  BPHomeUserView.swift
//  BPWeiBo
//
//  Created by 扒皮大叔 on 16/4/11.
//  Copyright © 2016年 apple. All rights reserved.
//用户原创微博的视图,只需要在这里懒加载设置约束就行

import UIKit
import SnapKit
import YYText
import SVProgressHUD
let StatusCellMargin: CGFloat = 8
class BPHomeUserView: UIView {
    
    //定义记录的约束,
    var bottomPicture:Constraint?
    
    //在视图中加载模型色数据,将模型的数据赋值给我们view的控件
    var statusCellViewModel:BPStatusCellViewModel? {
        
        didSet{
        
            timeLabel.text = statusCellViewModel?.time
            sourceLabel.text = statusCellViewModel?.source
            userIconImg.sd_setImageWithURL(statusCellViewModel!.headImgURl)
            nameLabel.text = statusCellViewModel!.statusListModel?.user?.name
            //contentLabel.text = statusCellViewModel!.statusListModel?.text
            
            //将原创微博的正文的文本替换成属性文本
            contentLabel.attributedText = statusCellViewModel?.huatiAtteibute
            
            let a:NSNumber = (statusCellViewModel?.verified_type)!
            
            switch a {
            case -1:
                userVerifiedImg.image = UIImage(named: String())
            case 2,3,5:
                userVerifiedImg.image = UIImage(named: "avatar_enterprise_vip")
            case 1:
                userVerifiedImg.image = UIImage(named: "avatar_vip")
            case 220:
                userVerifiedImg.image = UIImage(named: "avatar_grassroot")
            default : break
            }
            
            let b:NSNumber = (statusCellViewModel?.mbrank)!
            //print(b)
            if b == 0 {
                userRankImg.image = UIImage(named: "common_icon_membership_expired")

            }
            userRankImg.image = UIImage(named: "common_icon_membership_level\(b)")
            
            
            //在这里判断原创微博的图片视图是否存在,根据它是否存在来改变原创微博底部的约束和图片视图的约束
            //更新约束之前先把原来的约束卸载掉
            self.bottomPicture?.uninstall()
            if let urls = statusCellViewModel?.statusListModel?.pic_urls where urls.count > 0  {
                //让图片视图隐藏
                pictureView.hidden = false
                //让图片视图的数据源为空
                pictureView.pictureViewImg = urls
                //更新视图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomPicture = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }else {
                //让图片视图显示
                pictureView.hidden = true
                //设置图片视图的数据源
                pictureView.pictureViewImg = nil
                //更新视图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomPicture = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    
    }
    
    
    //将控件加载进入原创的视图
    override init(frame: CGRect) {
        super.init(frame:frame)
        backgroundColor = UIColor.whiteColor()
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:加载控件,社会中约束
    private func setUpUI() {
        
        //头像 的圆角
        userIconImg.cornerRadius = 20
        //加入视图中
        addSubview(userIconImg)
        addSubview(nameLabel)
        addSubview(userRankImg)
        addSubview(userVerifiedImg)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        
        //MARK:加载图片的视图
        addSubview(pictureView)
        
        
        //设置约束
        //头像的约束
        self.userIconImg.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSizeMake(40, 40))
            make.left.equalTo(self).offset(StatusCellMargin)
        }
        //微博名字的约束
        self.nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userIconImg.snp_top)
            make.left.equalTo(userIconImg.snp_right).offset(StatusCellMargin)
        }
        //用户的等级的约束
        self.userRankImg.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }
        //用户认证类型的约束
        self.userVerifiedImg.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(userIconImg.snp_right)
            make.bottom.equalTo(userIconImg.snp_bottom)
        }
        //设置时间的约束
        self.timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(userIconImg.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(userIconImg.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(userIconImg.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userIconImg.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(userIconImg.snp_left)
        }
        
        //MARK:设置图片视图的约束
        pictureView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            //make.right.equalTo(self).offset(-StatusCellMargin)
            //make.height.width.equalTo(100)
            //make.size.equalTo(CGSize(width: 100, height: 100))
        })
        
        //设置原创微博的底部相对微博正文的底部约束
        self.snp_makeConstraints { (make) -> Void in
            //在设置约束的时候记录一下
           self.bottomPicture =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        //处理点击事件  如果点击的是url  就跳转页面
        contentLabel.highlightTapAction = { (containerView, text, range, rect) -> () in
            //将属性文本转换为字符串
            let str = text.string
            //截取子串 获取对应点击的字符串
            let subStr = (str as NSString).substringWithRange(range)
            SVProgressHUD.showInfoWithStatus(subStr)
            if subStr.containsString("http") {
                //是url 跳转页面  视图不能够跳转页面
                let web = ViewController()
                web.urlString = subStr
                //跳转页面
                self.navController()?.pushViewController(web, animated: true)
            }
        }
        
    
    }
    
    //三个图片,4个label
    ///用户的头像
    private lazy var userIconImg:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    ///用户的等级
    private lazy var userRankImg:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    ///用户的认证类型
    private lazy var userVerifiedImg:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    ///微博发布的时间
    private lazy var timeLabel:UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    ///用户名
    private lazy var nameLabel:UILabel = UILabel(text: "周扒皮", textColor: UIColor.blackColor(), fontSize: 15)
    ///微博来源
    private lazy var sourceLabel:UILabel = UILabel(text: "隔壁老王", textColor: UIColor.blackColor(), fontSize: 12)
    ///微博的征文
    //private lazy var contentLabel:UILabel = UILabel(text:"hahhah", textColor: UIColor.darkGrayColor(), fontSize: 12, margin: StatusCellMargin)
    
    //MARK:利用YY的label来存放微博的征文
    private lazy var contentLabel:YYLabel = {
        let yy = YYLabel()
        //设置最大的布局宽度
        yy.preferredMaxLayoutWidth = ScreenWidth - 2*StatusCellMargin
        //设置行数为自动换行
        yy.numberOfLines = 0
        
        return yy
    }()
    
    //懒加载图片视图
    lazy var pictureView:BPHomePictureView = BPHomePictureView()
    
}

